package com.neusoft.databus.sap.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.neusoft.bizcore.common.cache.CacheService;
import com.neusoft.bizcore.common.utils.CacheUtil;
import com.neusoft.databus.common.constant.DatabusConstant;
import com.neusoft.databus.sap.service.ExMonitorDataService;

/**
 * 监控数据服务 <br>
 * 异步接收消息总线发送的消息 {#processMonitorDataEvent}
 *
 * @author sunchf
 */
@Service
public class ExMonitorDataServiceImpl implements ExMonitorDataService {

    @Autowired
    private CacheService cacheService;

    private String getMonitorDataKey(final String resourceCode) {
        final StringBuffer key = new StringBuffer();
        key.append(CacheUtil.keyBuilder(resourceCode));
        return key.toString();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> findInCache(final List<String> resourceCodeList) {

        final List<Map<String, Object>> result = new ArrayList<>();
        resourceCodeList.stream().distinct().forEach(code -> {
            Map<String, Object> data = Maps.newHashMap();
            final String key = this.getMonitorDataKey(code);
            if (this.cacheService.isInCache(DatabusConstant.CACHE_NAME_MONITOR_DATA, key)) {
                data = (Map<String, Object>) this.cacheService.get(DatabusConstant.CACHE_NAME_MONITOR_DATA, key);
            }
            data.put("code", code);
            result.add(data);
        });

        return result;
    }

}
